/**
 * @author helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.TransferBillDao) SALE.dao.local.TransferBillDao = function(){
	var billDoc;
}

SALE.dao.local.TransferBillDao.prototype = {
		/**
		 * 
		 */
		initBillDoc : function() {
			if(!this.billDoc) {
				this.billDoc = LocalXMLFileUtil.getTransferBillDoc();
			}
			
			return this.billDoc;
		},
		
		saveBillDoc : function() {
			LocalXMLFileUtil.saveXML(this.billDoc,LocalXMLFileUtil.getTransferBillFile());
		},
		
		insertBillInfo : function(billJson) {
			var root = this.billDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			billJson["id"] = id;
			root.setAttribute("maxid",id);
			var billNode = this.billDoc.createElement("bill");
			
			this.updateBill(billJson, billNode);			
			root.appendChild(billNode);
			
			this.saveBillDoc();
		},
		
		modifyBillInfo : function(billJson) {
			var billNode = this.searchBillByID(billJson.id);
			
			this.updateBill(billJson, billNode);
			
			this.saveBillDoc();
		},
		
		deleteBillInfo : function(id) {
			var billNode = this.searchBillByID(id);
			var root = this.billDoc.documentElement;
			if(billNode != null) {
				root.removeChild(billNode);
			}			
		},
		
		updateBill : function(billJosn, billNode) {
			for (var attri in billJosn) {
			
				if (attri.indexOf("old") != 0 && billJosn[attri] && billJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, billNode);
					if (attriNodes != null && attriNodes.length > 0) {
						billNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.billDoc.createElement(attri);
					billNode.appendChild(attriNode);
					
					if (attri == "details") {
						var details = billJosn.details;						
						for (var i = 0; i < details.length; i++) {
							var detail = details[i];
							var detailNode = this.billDoc.createElement("detail");
							attriNode.appendChild(detailNode);
							
							for (var attriDetail in detail) {
							
								var detailAttriNode = this.billDoc.createElement(attriDetail);
								contextNode = this.billDoc.createCDATASection(detail[attriDetail]);
								detailAttriNode.appendChild(contextNode);
								detailNode.appendChild(detailAttriNode);
							}
						}
					} else {						
						var contextNode = this.billDoc.createCDATASection(billJosn[attri]);
						attriNode.appendChild(contextNode);
					}
				}
				
			}	
		},
		
		searchBillByID : function(id) {
			var root = this.billDoc.documentElement;
			var objArray = Ext.query("bill:has(id:nodeValue(" + id+ "))", root);
			var result;
			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchBillByCode : function(code) {
			var root = this.billDoc.documentElement;
			var objArray = Ext.query("bill:has(code:nodeValue(" + code + "))", root);
			var result;
			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		/**
		 * 修改调拨单接收数量。
		 * @param {Object} receiveJson
		 */
		modifyBillProductReceiveNumber : function(receiveJson) {
			var bill = this.searchTransferBillJsonByCode(receiveJson.bcode);
				
			if(bill) {
				var details = bill.details;
				var endFlag = true;
				for(var i = 0; i < details.length; i++) {
					var detail = details[i];
					if(receiveJson.index) {
						if(detail.index == receiveJson.index) {
							var pnumber = detail.pnumber != null && detail.pnumber != "" ? parseFloat(detail.pnumber) : 0;;
							var rnumber = detail.receive_number != null && detail.receive_number != "" ? parseFloat(detail.receive_number) : 0;
							var number = receiveJson.number;
							rnumber = parseFloat(rnumber) + parseFloat(number);
							detail.receive_number = rnumber;
						}
					} else if(detail.pcode == receiveJson.pcode) {
						var pnumber = detail.pnumber != null && detail.pnumber != "" ? parseFloat(detail.pnumber) : 0;;
						var rnumber = detail.receive_number != null && detail.receive_number != "" ? parseFloat(detail.receive_number) : 0;
						var number = receiveJson.number;
						rnumber = parseFloat(rnumber) + parseFloat(number);
						detail.receive_number = rnumber;
					}
					
					if(detail.receive_number != detail.pnumber) {
						endFlag = false;
					}
				}
				
				if(endFlag) {
					bill.end_flag = 1;
				}
				
				this.modifyBillInfo(bill);
			}
		},
		
		/**
		 * 根据ID，查询出调拨单所有详细信息，并封装成JSON对象。
		 * @param {Object} code
		 */
		searchTransferBillJson : function(id) {
			var bill = this.searchBillByID(id);
			
			if (bill) {
				var children = bill.childNodes;
				var billJson = {};
				
				for (var i = 0; i < children.length; i++) {
					var child = children[i];
					
					if (child.nodeType == 1) {
						if (child.nodeName == "details") {
							
							if (!billJson.details) {
								billJson.details = [];
							}
							
							var detailArray = Ext.query("/detail", child);
							
							Ext.each(detailArray, function(detail) {
								var attriArray = detail.childNodes;
								var detailJson = {}
								for (var j = 0; j < attriArray.length; j++) {
									var attri = attriArray[j];
									if (attri.nodeType == 1) {
										if (!detailJson[attri.nodeName] && attri.childNodes[0] != null) {
											detailJson[attri.nodeName] = attri.childNodes[0].nodeValue;
										}
									}
								}
								
								billJson.details.push(detailJson);								
							});
						} else {
							
							if (!billJson[child.nodeName] && child.childNodes[0] != null) {
								billJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}
					}
				}	
				
				return billJson;
			}
		},
		
		/**
		 * 根据编号，查询出调拨单所有详细信息，并封装成JSON对象。
		 * @param {Object} code
		 */
		searchTransferBillJsonByCode : function(code) {
			var bill = this.searchBillByCode(code);
			
			if (bill) {
				var children = bill.childNodes;
				var billJson = {};
				
				for (var i = 0; i < children.length; i++) {
					var child = children[i];
					
					if (child.nodeType == 1) {
						if (child.nodeName == "details") {
							
							if (!billJson.details) {
								billJson.details = [];
							}
							
							var detailArray = Ext.query("/detail", child);
							
							Ext.each(detailArray, function(detail) {
								var attriArray = detail.childNodes;
								var detailJson = {}
								for (var j = 0; j < attriArray.length; j++) {
									var attri = attriArray[j];
									if (attri.nodeType == 1) {
										if (!detailJson[attri.nodeName] && attri.childNodes[0] != null) {
											detailJson[attri.nodeName] = attri.childNodes[0].nodeValue;
										}
									}
								}
								
								billJson.details.push(detailJson);								
							});
						} else {
							
							if (!billJson[child.nodeName] && child.childNodes[0] != null) {
								billJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}
					}
				}	
				
				return billJson;
			}
		},
		
		/**
		 * 根据输入条件，查询出调拨单基本信息。并封闭成数组。
		 * @param {Object} condition
		 */	
		searchTransferBillsArray : function(condition) {
			var root = this.billDoc.documentElement;
			var objArray;
			
			if(!condition.status || condition.status == "2") {
				objArray = Ext.query("bill", root);
			} else {
				if(condition.status == "1") {
					objArray = Ext.query("bill:has(status:nodeValue(true))", root);
				} else {
					objArray = Ext.query("bill:not(bill:has(status:nodeValue(true)))", root);
				}				
			}
			
			var result = [];
		    Ext.each(objArray, function(bill) {
				var filterFlag = false;
				for (var attri in condition) {
					if(attri != "status") {
						var tempAttri = attri.replace("_to", "");
						tempAttri = tempAttri.replace("_from", "");
						var node = Ext.DomQuery.select("/"+ tempAttri +"/*", bill)[0];
						if(condition[attri] != null && condition[attri] != "") {
							if(!node || node.nodeValue == null || node.nodeValue == "") {
								filterFlag = true;
								break;
							}
							
							if(attri.indexOf("date") > -1 && attri.indexOf("from") > -1) {
								if(Date.parse(node.nodeValue) < Date.parse(condition[attri].replaceAll("-", "/"))) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("_from") > -1) {
								if(node.nodeValue < condition[attri]) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("date") > -1 && attri.indexOf("_to") > -1) {
								if(Date.parse(node.nodeValue) > Date.parse(condition[attri].replaceAll("-", "/"))) {
									filterFlag = true;
									break;
								}
							} else if(attri.indexOf("_to") > -1) {
								if(node.nodeValue > condition[attri]) {
									filterFlag = true;
									break;
								}
							} else {
								if(node.nodeValue.toUpperCase().indexOf(condition[attri].toUpperCase()) < 0) {
									filterFlag = true;
									break;
								}
							}
						}
					}
				}
				
				if(!filterFlag) {
					var children = bill.childNodes;
					var nodeJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!nodeJson[child.nodeName] && child.childNodes[0] != null) {
								nodeJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					result.push(nodeJson);
				}

		    });
			return result;
		},
		
		/**
		 * 根据输入条件，查询出调拨单的所有信息，并封闭成数组。
		 * @param {Object} condition
		 */
		searchTransferBillDetailsArray : function(condition) {
			if(!this.billDoc) {
				this.initBillDoc();
			}
			var root = this.billDoc.documentElement;
			var objArray = Ext.query("bill", root);
			var result = [];
		    Ext.each(objArray, function(bill) {
				var filterFlag = false;
				for (var attri in condition) {
					var node = Ext.DomQuery.select("/"+ attri +"/*", bill)[0];
					if(condition[attri] != null && node != null) {
						if(attri.indexOf("date") > -1 && attri.indexOf("from") > -1) {
							if(Date.parse(node.nodeValue) < Date.parse(condition[attri])) {
								filterFlag = true;
							}
						} else if(attri.indexOf("from") > -1) {
							if(node.nodeValue < condition[attri]) {
								filterFlag = true;
							}
						} else if(attri.indexOf("date") > -1 && attri.indexOf("to") > -1) {
							if(Date.parse(node.nodeValue) > Date.parse(condition[attri])) {
								filterFlag = true;
							}
						} else if(attri.indexOf("to") > -1) {
							if(node.nodeValue > condition[attri]) {
								filterFlag = true;
							}
						} else {
							if(node.nodeValue.indexOf(condition[attri]) < 0) {
								filterFlag = true;
							}
						}
					}
				}
					
				if(bill) {
					var children = bill.childNodes;
					var nodeJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if (child.nodeName == "details") {
							
							if (!nodeJson.details) {
								nodeJson.details = [];
							}
							
							var detailArray = Ext.query("/detail", child);
							
							Ext.each(detailArray, function(detail) {
								var attriArray = detail.childNodes;
								var detailJson = {}
								for (var j = 0; j < attriArray.length; j++) {
									var attri = attriArray[j];
									if (attri.nodeType == 1) {
										if (!detailJson[attri.nodeName] && attri.childNodes[0] != null) {
											detailJson[attri.nodeName] = attri.childNodes[0].nodeValue;
										}
									}
								}
								
								nodeJson.details.push(detailJson);
													
							});
						} else {
							
							if (!nodeJson[child.nodeName] && child.childNodes[0] != null) {
								nodeJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}					
					}
					
					result.push(nodeJson);
				}

		    });
			return result;
		}
}